﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace db
{
    public class DbHelper
    {
        public static string connstr = @"server=localhost; User Id=sa;password=sa; Database=dbcaac_sql_server;";
        public System.Data.SqlClient.SqlConnection conn = null;
        #region "数据库设置"

        /// <summary>
        /// 获取数据库类型
        /// </summary>

        public static string GetDBType
        {
            get
            {
                return System.Configuration.ConfigurationSettings.AppSettings["DBType"];
            }
        }
        /// <summary>
        /// 获取数据库连接字符串
        /// </summary>
        public static string GetConnString
        {
            get
            {
                return System.Configuration.ConfigurationSettings.AppSettings[GetDBType];// +Get_UserCompanyCode;
            }
        }
        #endregion
        public DbHelper()
        {
            connstr = GetConnString;
           //
        }
        public DbHelper(string connstring)
        {
            connstr = connstring;
        }
        //重载
        /// <summary>
        /// 默认连接
        /// </summary>
        /// <returns>SqlConnection</returns>
        public System.Data.SqlClient.SqlConnection getConn()
        {  
            return new System.Data.SqlClient.SqlConnection(connstr);
        }
        /// <summary>
        /// 建立连接
        /// </summary>
        /// <param name="connstring">给定连接字符串</param>
        /// <returns>SqlConnection</returns>
        public System.Data.SqlClient.SqlConnection getConn(string connstring){
            return new System.Data.SqlClient.SqlConnection(connstring);
        }
        /// <summary>
        /// 建立连接
        /// </summary>
        /// <param name="connstring">给定连接字符串</param>
        /// <returns>SqlConnection</returns>
        public System.Data.SqlClient.SqlConnection getConn(string host ,string db,string userid ,string password)
        {
            return null;
           // return new System.Data.SqlClient.SqlConnection(connstring);
        }
        /// <summary>
        /// 执行查询
        /// </summary>
        /// <param name="strCmd"></param>
        /// <returns></returns>
        public int RunNoQuery(string sql)
        {
            int x = 0;
            System.Data.SqlClient.SqlCommand cmd =new System.Data.SqlClient.SqlCommand();
            System.Data.SqlClient.SqlConnection con = getConn();
            cmd.Connection = con;
            cmd.CommandText = sql;
            cmd.CommandType = System.Data.CommandType.Text;
            con.Open();
            x=   cmd.ExecuteNonQuery();
            con.Close();
            return x;
        }
        /// <summary>
        /// 执行查询
        /// </summary>
        /// <param name="strCmd"></param>
        /// <returns></returns>
        public System.Data.DataTable RunQuery(string sql)
        {
            System.Data.DataTable dt = new System.Data.DataTable();
            System.Data.SqlClient.SqlDataAdapter adp = new System.Data.SqlClient.SqlDataAdapter();
            adp.SelectCommand = new System.Data.SqlClient.SqlCommand(sql, getConn());
            Console.WriteLine(sql);
            adp.Fill(dt);
            return dt;
        }
        public System.Data.DataTable Tables
        {
            get { return conn.GetSchema("Tables", null); }
        }

        public System.Data.DataTable GetColumns()
        {
            return conn.GetSchema("Columns", null);
        }
       
        public  System.Data.DataTable GetColumns(string strTable)
        {
            return conn.GetSchema("Columns", new string[] { null, null, strTable, null });
        }
    }
}
